package com.example.demo.web.controller;

import com.example.demo.service.ResourceService;
import com.example.demo.vo.ResultData;
import com.qiniu.common.QiniuException;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("resources")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    @GetMapping("token")
    public ResultData getResourceToken(){
        String accessKey = "A5IkKjSH4NKH-L4VjAbvoSLOEKvdIcgyA3A-19n_";
        String secretKey = "hdNtvbguDpajtf0XYh_bubmQf6KwuXDs1q1-c3vV";
        String bucket = "class10";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        return ResultData.success(upToken);
    }


    @PostMapping("upload")
    public ResultData uploadFile(MultipartFile file) throws QiniuException {
        boolean flag = resourceService.upload(file);
        return ResultData.success(flag);
    }



}
